//
//  STDiscoverView.swift
//  STVVeibo
//
//  Created by leergou on 16/7/23.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

class STDiscoverView: UIView {


    override init(frame: CGRect) {
        super.init(frame: CGRectMake(0, 0, SCREENW, 35))
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 按钮点击方法
    func cancelButtonClick(){
        
        // 更新约束
        self.textField.snp_updateConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.layoutIfNeeded()
                }, completion: { (_) -> Void in
                    self.textField.resignFirstResponder()
            })
        }
    }
    
    //MARK: setupUI
    func setupUI(){
        //添加控件
        addSubview(cancelButton)
        addSubview(textField)
        // 添加约束
        cancelButton.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(50)
            make.top.bottom.trailing.equalTo(self)
        }
        textField.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
    
    //MARK: lazy
    private lazy var cancelButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "cancelButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("取消", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font =  UIFont.systemFontOfSize(15)
        return button
    }()
    
    private lazy var textField: UITextField = {
        // leftView
       let img = UIImageView(imgName: "searchbar_textfield_search_icon")
        img.contentMode = .Center
        img.frame.size = CGSize(width: 35, height: 35)
        // textField
        let txt = UITextField()
        txt.layer.borderColor = ThemeColor.CGColor
        txt.layer.borderWidth = 2
        txt.placeholder = "搜索万万千"
        txt.backgroundColor = UIColor.whiteColor()
        txt.leftView = img
        txt.leftViewMode = .Always
        txt.delegate = self
        return txt
    }()
}

extension STDiscoverView:UITextFieldDelegate{
    func textFieldDidBeginEditing(textField: UITextField) {
        
        self.textField.snp_updateConstraints { (make) -> Void in
            make.trailing.equalTo(self).offset(-50)
        }
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
}








